/*=============================================================================

  Library: CFS

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/

#include "CFSPluginArchiveSQL_p.h"

#include "CFSPluginException.h"
#include "CFSPluginStorageSQL_p.h"
#include "CFSPluginDatabaseException.h"

#include <QStringList>
#include <QFile>


//----------------------------------------------------------------------------
CFSPluginArchiveSQL::CFSPluginArchiveSQL(CFSPluginStorageSQL* pluginStorage,
                                         const QUrl& pluginLocation, const QString& localPluginPath,
                                         int pluginId, int startLevel, const QDateTime& lastModified,
                                         int autostartSetting)
  : key(-1), autostartSetting(autostartSetting), id(pluginId), generation(0)
  , startLevel(startLevel), lastModified(lastModified), location(pluginLocation)
  , localPluginPath(localPluginPath), storage(pluginStorage)
{
}

//----------------------------------------------------------------------------
CFSPluginArchiveSQL::CFSPluginArchiveSQL(QSharedPointer<CFSPluginArchiveSQL> old, int generation,
                                         const QUrl &pluginLocation, const QString &localPluginPath)
  : key(-1), autostartSetting(old->autostartSetting), id(old->id), generation(generation)
  , startLevel(0), location(pluginLocation), localPluginPath(localPluginPath)
  , storage(old->storage)
{
}

void CFSPluginArchiveSQL::readManifest(const QByteArray& manifestResource)
{
  QByteArray manifestRes = manifestResource.isNull() ? this->getPluginResource("META-INF/MANIFEST.MF")
                                                  : manifestResource;
  if (manifestRes.isEmpty())
  {
    throw CFSPluginException(QString("CFSPlugin has no MANIFEST.MF resource, location=") + localPluginPath);
  }

  manifest.read(manifestRes);
}

//----------------------------------------------------------------------------
QString CFSPluginArchiveSQL::getAttribute(const QString& key) const
{
  return manifest.getAttribute(key);
}

//----------------------------------------------------------------------------
QHash<QString,QString> CFSPluginArchiveSQL::getUnlocalizedAttributes() const
{
  return manifest.getMainAttributes();
}

int CFSPluginArchiveSQL::getPluginGeneration() const
{
  return generation;
}

//----------------------------------------------------------------------------
int CFSPluginArchiveSQL::getPluginId() const
{
  return id;
}

//----------------------------------------------------------------------------
QUrl CFSPluginArchiveSQL::getPluginLocation() const
{
  return location;
}

//----------------------------------------------------------------------------
QString CFSPluginArchiveSQL::getLibLocation() const
{
  return localPluginPath;
}

//----------------------------------------------------------------------------
QByteArray CFSPluginArchiveSQL::getPluginResource(const QString& component) const
{
  try
  {
    return storage->getPluginResource(key, component);
  }
  catch (const CFSPluginDatabaseException& exc)
  {
    qDebug() << QString("Getting plugin resource %1 failed:").arg(component) << exc;
    return QByteArray();
  }
}

//----------------------------------------------------------------------------
QStringList CFSPluginArchiveSQL::findResourcesPath(const QString& path) const
{
  try
  {
    return storage->findResourcesPath(key, path);
  }
  catch (const CFSPluginDatabaseException& exc)
  {
    qDebug() << QString("Getting plugin resource paths for %1 failed:").arg(path) << exc;
  }
  return QStringList();
}

//----------------------------------------------------------------------------
int CFSPluginArchiveSQL::getStartLevel() const
{
  return startLevel;
}

//----------------------------------------------------------------------------
void CFSPluginArchiveSQL::setStartLevel(int level)
{
  if (startLevel != level)
  {
    startLevel = level;
    storage->setStartLevel(key, level);
  }
}

//----------------------------------------------------------------------------
QDateTime CFSPluginArchiveSQL::getLastModified() const
{
  return lastModified;
}

//----------------------------------------------------------------------------
void CFSPluginArchiveSQL::setLastModified(const QDateTime& dateTime)
{
  lastModified = dateTime;
  storage->setLastModified(key, dateTime);
}

//----------------------------------------------------------------------------
int CFSPluginArchiveSQL::getAutostartSetting() const
{
  return autostartSetting;
}

//----------------------------------------------------------------------------
void CFSPluginArchiveSQL::setAutostartSetting(int setting)
{
  if (autostartSetting != setting)
  {
    autostartSetting = setting;
    storage->setAutostartSetting(key, setting);
  }
}

//----------------------------------------------------------------------------
void CFSPluginArchiveSQL::purge()
{
  storage->removeArchive(this);
}

//----------------------------------------------------------------------------
void CFSPluginArchiveSQL::close()
{

}
